package com.zhihuichongcheng.application.ui.task.adapter.holder;

import android.content.Intent;
import android.os.Parcel;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.smartlib.cmnObject.util.DateUtil;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.bean.cricle.CricleImageInfo;
import com.zhihuichongcheng.application.bean.task.TaskCommentBean;
import com.zhihuichongcheng.application.glide.GlideCircleTransform;
import com.zhihuichongcheng.application.ui.cricle.activity.ShowImageActivity;
import com.zhihuichongcheng.application.util.CollectionUtils;
import com.zhihuichongcheng.application.util.StringUtils;

import java.util.ArrayList;

/**
 * TaskCommentHolder Created by LK on 2017/1/19.
 */

public class TaskCommentHolder extends BaseHolder {
    private ImageView mUserImg;
    private TextView mTvNickName, mTvContent, mTvTime;
    private LinearLayout mImgLayout;


    public TaskCommentHolder(View itemView, BaseActivity activity) {
        super(itemView, activity);
        mUserImg = (ImageView) itemView.findViewById(R.id.user_img);
        mTvNickName = (TextView) itemView.findViewById(R.id.comment_user);
        mTvContent = (TextView) itemView.findViewById(R.id.comment_content);
        mTvTime = (TextView) itemView.findViewById(R.id.comment_time);
        mImgLayout = (LinearLayout) itemView.findViewById(R.id.img_layout);
    }

    public void setData(TaskCommentBean commentBean) {
        Glide.with(getContext()).load(commentBean.getUserLogo()).asBitmap().placeholder(R.drawable.ic_default_avatar).
                transform(new GlideCircleTransform(getContext())).into(mUserImg);

        mTvNickName.setText(commentBean.getUserName());
        mTvTime.setText(DateUtil.showDate(commentBean.getCreateTime()));
        mTvContent.setText(commentBean.getContent());
        setImgViews(commentBean.getImage());
    }

    /**
     * 设置图片
     */
    private void setImgViews(String imgs) {
        if (StringUtils.isEmpty(imgs)) return;
        mImgLayout.removeAllViews();
        String[] imgUrls = imgs.split(",");
        CollectionUtils.iterator(imgUrls, new CollectionUtils.AIIteratorListCallBack() {
            @Override
            public boolean callback(Object data, int position) {
                String imgUrl = data.toString();
                mImgLayout.addView(getItemImgView(imgUrl, mImgLayout));
                return false;
            }
        });

    }

    private View getItemImgView(final String imgUrl, LinearLayout mImgLayout) {
        View itemView = LayoutInflater.from(mImgLayout.getContext()).inflate(R.layout.view_comment_img, mImgLayout, false);
        ImageView imgView = (ImageView) itemView.findViewById(R.id.img);
        Glide.with(getContext()).load(imgUrl).asBitmap().into(imgView);
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), ShowImageActivity.class);
                final ArrayList<CricleImageInfo> images = new ArrayList<>();
                Parcel parcel = Parcel.obtain();
                parcel.writeString(imgUrl);
                CricleImageInfo cricleImageInfo = new CricleImageInfo(parcel);
                cricleImageInfo.setJinbaoHomeImgUrl(imgUrl);
                images.add(cricleImageInfo);
                intent.putParcelableArrayListExtra("list", images);
                getContext().startActivity(intent);
                activity.overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
            }
        });
        return itemView;
    }
}
